/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: Mapview.qml.
*/

import QtQuick 1.0
import QtMobility.location 1.2
import Qt.labs.gestures 1.0
import MyGestureArea 1.0

/*!
 *  \file
 *  \brief Defines Map view to show current or bookmark location on the map.
 *
 *  Map view consists of two button elements and a Map element that
 *  updates the location on the map. It also uses LocationGestureArea custom
 *  element to provide pich zoom.
 *
 *  It uses "locationEngine" context properties to start a new position update
 *  request.
 */

Rectangle {
    id: myMapview
    anchors.fill:  parent
    color: "green"
    focus : true

    // Publish coordinate. Used by the Map center property.
    property Coordinate mapCenter: Coordinate {
                                latitude: 0.25
                                longitude: 0.51
                                }

    Connections {
        target: locationManager
        onPositionChanged: {
            mapCenter.latitude = locationManager.latitude;
            mapCenter.longitude = locationManager.longitude;
            topMostView.showDialog = showDialog;
        }
    }

    Map {
        id: locationMap

        plugin : Plugin {
            name : "nokia"
        }

        size.width: parent.width
        size.height: parent.height
        zoomLevel: 12
        center: mapCenter

        MapCircle {
            id: myPosition
            color: "red"
            radius: 20
            center: mapCenter
        }

        MapText {
            id: myMapText
            color: "blue"
            coordinate: mapCenter
            offset.y: myPosition.radius
            text: locationManager.locationName
            font.pointSize: 8
        }
    }

    LocationGestureArea {
        id: mapScaler
        anchors.fill: parent
        focus: true
        z: 1
        property double oldzoom

        // Calculate the zoom level on the map.
        function calcZoomDelta(zoom, percent) {
            return zoom + Math.log(percent)/Math.log(2)
        }
        onStarted: {
            oldzoom = locationMap.zoomLevel;
        }

        onMapScaleChanged: {
            // Set new zoom level.
            locationMap.zoomLevel = calcZoomDelta(oldzoom, mapScaler.mapScale)
        }
    }

    Rectangle {
        width: parent.width
        height: 60
        y: parent.height - 60
        color:  "gray"
        opacity: 0.8

        Button {
            x: 5
            label: "My position"
            visible: (showDialog == false && showLocations == false)
            onButtonClicked:  {
                locationManager.startLocationUpdate();
                locationManager.locationName = "";
            }
        }

        Button {
            x: parent.width / 2 + 5
            visible: (showDialog == false && showLocations == false)
            label: "Bookmarks"
            onButtonClicked: topMostView.showLocations = true;
        }
    }
}


    


